﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCG.Engine.Core;
using TCG.Engine.Effects;
using TCG.Engine.Impacts;
using TCG.Engine.Libraries;
using TCG.Engine.WiningConditions;
using TCG.Engine;
using System.Threading;

namespace TCGWeb
{

    public partial class _Default : System.Web.UI.Page
    {
        int countCardsInHand = 3;

        protected WebPlayerInteraction ThreadInteraction
        {
            get
            {
                var threadInteraction = Session["ThreadInteraction"] as WebPlayerInteraction;
                if (threadInteraction == null)
                {
                    threadInteraction = new WebPlayerInteraction(Session);
                    threadInteraction.InitializeMatch = InitializeMatch;
                    Session["ThreadInteraction"] = threadInteraction;
                }
                return threadInteraction;
            }
        }

        private void InitializeMatch()
        {
            Env.Reg<ICardLibrary>(new FileCardLibrary(Constants.CardsFileName));
            Env.Reg<IDeckLibrary>(new FileDeckLibrary(Constants.DecksFileName));
            var el = new EffectLibrary();
            el.LoadAll(typeof(EffectLibrary).Assembly);
            Env.Reg<IEffectLibrary>(el);

            var matchOptions = new MatchOptions()
            {
                Player1 = new PlayerOptions()
                {
                    Name = "Player1",
                    StartGold = 50,
                    StartMana = 20,
                    StartGoldIncome = 10,
                    StartManaIncome = 5,
                    StartLife = 30,
                    DeckId = "TestDeck1"
                },

                Player2 = new PlayerOptions()
                {
                    Name = "Player2",
                    StartGold = 50,
                    StartMana = 20,
                    StartGoldIncome = 10,
                    StartManaIncome = 5,
                    StartLife = 30,
                    DeckId = "TestDeck1",
                },
                StartCardsCount = countCardsInHand,
                MaxCardsInHand = countCardsInHand,
            };

            matchOptions.WinningCondition = new WCOr( new WCReduceLife(0,TargerInfo.Enemy), new WCGrowLife(50,TargerInfo.Self));

            ThreadInteraction.TCGMatch.Start(matchOptions);

            ThreadInteraction.TCGMatch.Player1.Interaction = ThreadInteraction;
            ThreadInteraction.TCGMatch.Player2.Interaction = ThreadInteraction;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ThreadInteraction.TCGMatch.MatchResult.GameOver)
                lblGameResult.Text = "Игра окончена!" + Environment.NewLine + " Результат - " + (ThreadInteraction.TCGMatch.MatchResult.IsDraw ? " ничья :)" : " победитель " + ThreadInteraction.TCGMatch.MatchResult.Winner.Name);
        }

        protected void lbtCard_Command(object source, CommandEventArgs e)
        {
            bool turnResult = ThreadInteraction.TCGMatch.PlayerMove((string)e.CommandArgument);
            if (!turnResult || !ThreadInteraction.TCGMatch.ActivePlayer.Hand.Any())
                NextTurn();
        }

        protected void btnEndTurn_Click(object sender, EventArgs ea)
        {
            NextTurn();
        }

        private void NextTurn()
        {
            ThreadInteraction.TCGMatch.EndTurn();
            ThreadInteraction.TCGMatch.StartTurn();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            //rptLT.DataSource = TCGMatch.LastTurn;
            //rptLT.DataBind();

            rptCards.DataSource = ThreadInteraction.TCGMatch.ActivePlayer.Hand;
            rptCards.DataBind();
        }

        protected String _logLines;
        protected String LogLines
        {
            get
            {
                if (_logLines == null)
                {
                    _logLines = (string)ViewState["LogLines"];
                    if (_logLines == null)
                        ViewState["LogLines"] = _logLines = String.Empty;

                }
                return _logLines;
            }
            set { ViewState["LogLines"] = _logLines = value; }
        }

        private void LogLine(String line)
        {
            LogLines = line + "<br />" + LogLines;
        }

        protected string GetHoverClass(bool isNotActive)
        {
            return isNotActive ? "dark" : "over";
        }
    }
}
